package biz

import (
	"driver/cmd/basic/config"
	"gorm.io/gorm"
	"time"
)

// VehicleUser 司机管理人员管理
type VehicleUser struct {
	gorm.Model
	Name         string `gorm:"type:varchar(20);not null;comment:姓名"`
	Phone        string `gorm:"type:char(11);not null;comment:手机号"`
	IdCard       string `gorm:"type:char(18);not null;comment:身份证号"`
	PlateNumber  string `gorm:"type:varchar(20);not null;comment:车牌号"`
	ResidentCity string `gorm:"type:varchar(20);not null;comment:常驻城市"`
	Status       string `gorm:"type:varchar(20);default:审批中;not null;comment:状态 审批中 已启用"`
}

func (v *VehicleUser) VehicleList(page, pageSize int, Name string) (vehicleList []*VehicleUser, err error) {

	result := config.DB.Debug().Scopes(Paginate(page, pageSize)).Where("name like ?", "%"+Name+"%").Find(&vehicleList)
	if result.Error != nil {
		return nil, result.Error
	}

	return vehicleList, nil
}

func Paginate(page, pageSize int) func(db *gorm.DB) *gorm.DB {

	return func(db *gorm.DB) *gorm.DB {

		if page <= 0 {
			page = 1
			println("Page reset to 1")
		}

		originalPageSize := pageSize

		switch {
		case pageSize > 100:
			pageSize = 100
		case pageSize <= 0:
			pageSize = 10
			println("PageSize reset from", originalPageSize, "to 10")
		}

		offset := (page - 1) * pageSize
		return db.Offset(offset).Limit(pageSize)
	}

}

// DriverVehicle 司机详情信息表
type DriverVehicle struct {
	gorm.Model
	Name               string     `gorm:"type:varchar(20);not null;comment:司机姓名"`
	Gender             string     `gorm:"type:varchar(20);not null;comment:性别 男 女"`
	Education          string     `gorm:"type:varchar(20);not null;comment:学历"`
	ResidentCity       string     `gorm:"type:varchar(20);not null;comment:常驻城市"`
	IdType             string     `gorm:"type:varchar(20);not null;comment:身份证件"`
	IdNumber           string     `gorm:"type:varchar(20);not null;comment:证件号码"`
	ValidityEnd        *time.Time `gorm:"column:validity_end;type:date;index;comment:证件有效期结束日期（长期有效时为null）"` // 结束时间
	IdFrontImg         string     `gorm:"type:varchar(500);not null;comment:身份证正面图片"`
	IdHandheldImg      string     `gorm:"type:varchar(500);not null;comment:手持身份证照片"`
	IdBackImg          string     `gorm:"type:varchar(500);not null;comment:身份证反面照片"`
	PlateNumber        string     `gorm:"type:varchar(20);not null;comment:车牌号"`
	VehicleOwner       string     `gorm:"type:varchar(20);not null;comment:车辆所属人"`
	VehicleBrand       string     `gorm:"type:varchar(20);not null;comment:车辆品牌"`
	VehicleModel       string     `gorm:"type:varchar(20);not null;comment:车辆型号"`
	VehicleColor       string     `gorm:"type:varchar(500);not null;comment:驾驶证图片"`
	VehicleLicenseInfo string     `gorm:"type:varchar(20);not null;comment:行驶证信息"`
	VehicleImgs        string     `gorm:"type:varchar(500);not null;comment:车辆图片"`
}

func (d *DriverVehicle) ShowDriverVehicle() error {
	return config.DB.Where("id=?", d.ID).Find(&d).Error

}
